/*
 * $Id$
 *
 * Copyright (c) 2006, 2006 WorldTicket A/S
 * All rights reserved.
 */
package my.news;

import java.util.HashSet;
import java.util.Set;

import com.aliasi.chunk.Chunk;
import com.aliasi.chunk.Chunking;
import com.aliasi.dict.DictionaryEntry;
import com.aliasi.dict.ExactDictionaryChunker;
import com.aliasi.dict.MapDictionary;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;

/**
 * @author Yuriy V. Koroliov (YUK) / WorldTicket A/S
 * @version $Revision$ $Date$
 */
public class ProcessNewsContent implements DocumentSetProcessor {
	private Set<String> wordsToSearch = new HashSet<String>();
	private String companyName;
	public static final String INTEREST_WORD = "INTEREST_WORD";
	public static final String COMPANY = "COMPANY";
	private ExactDictionaryChunker cunker;

	public ProcessNewsContent(Set<String> wordsToSearch, String companyName) {
		super();
		this.wordsToSearch = wordsToSearch;
		this.companyName = companyName;

		MapDictionary<String> dictionary = new MapDictionary<String>();
		for(String word : wordsToSearch) {
			dictionary.addEntry(new DictionaryEntry<String>(word, INTEREST_WORD));
		}
		dictionary.addEntry(new DictionaryEntry<String>(companyName, COMPANY));

		cunker = new ExactDictionaryChunker(dictionary,
                                     IndoEuropeanTokenizerFactory.FACTORY,
                                     true,true);
	}



	/**
	 * select news
	 * @param contents
	 * @return
	 */
	public Set<Content> process(Set<Content> contents) {
		Set<Content> resultNews = new HashSet<Content>();
		for(Content content : contents) {
			Chunking chunking = cunker.chunk(content.data);
			Set<Chunk> chunks = chunking.chunkSet();
			if(chunks.size() < 2) {
				continue;
			}
			boolean contiansComapny = false;
			for (Chunk chunk : chunks) {
				if(chunk.type().equals(COMPANY)) {
					contiansComapny = true;
					break;
				}
			}
			if (!contiansComapny) {
				continue;
			}
			resultNews.add(content);
		}
		return resultNews;
	}
}
